package com.hxd.mall.module.account.verifycode

import com.hxd.mall.module.account.bean.LoginBean
import com.hxd.mall.network.base.BaseRequest
import com.hxd.mall.network.service.ManagerFunctionApi.mIAccountFunctionApi
import com.hxd.mall.utils.local.HxdSharePreferenceUtil
import com.hxd.mall.utils.local.RequestUtil
import com.hxd.mall.utils.local.ThirdInitUtil.bindJPush
import com.hxd.mall.utils.open.ToastUtil
import java.util.*
import javax.inject.Inject

/**
 * @author zhaoyong
 * @description:
 * @date :2019-10-15 15:27
 */
class VerifyCodePresenter @Inject constructor(var mView: IVerifyCodeView) : IVerifyCodePresenter {
    /**
     * type 正常登陆 type=1
     * 微信图标登陆 type=3
     */
    override fun login(mobile: String, verificationCode: String, type: Int) {
        val treeMap = TreeMap<String, Any>()
        treeMap["mobile"] = mobile
        treeMap["verificationCode"] = verificationCode
        treeMap["type"] = type
        //增加参数 wxAccessToken
        treeMap["wxAccessToken"] = HxdSharePreferenceUtil.getStringData("wxAccessToken")

        BaseRequest.request(mIAccountFunctionApi.login(RequestUtil.getRequestBody(treeMap)), mView, {
                //重新绑定极光
                if (it?.data?.alias != null && it.data.alias.isNotEmpty())
                    bindJPush(it.data.alias)
                mView.loginStatus(it)
            },
            {
                ToastUtil.showToast(it.msg ?: "")
            })
    }

    /**
     * type 正常1
     * 微信注册传2
     */
    override fun register(
        mobile: String,
        verificationCode: String,
        invitationCode: String,
        type: Int
    ) {
        val treeMap = TreeMap<String, Any>()
        treeMap["mobile"] = mobile
        treeMap["verificationCode"] = verificationCode
        treeMap["invitationCode"] = invitationCode
        treeMap["type"] = type.toString()
        //增加参数 wxAccessToken
        treeMap["wxAccessToken"] = HxdSharePreferenceUtil.getStringData("wxAccessToken")
        BaseRequest.request(mIAccountFunctionApi.register(RequestUtil.getRequestBody(treeMap)), mView, {
            //重新绑定极光
            if (it?.data?.alias != null && it.data.alias.isNotEmpty())
                bindJPush(it.data.alias)
            mView.registerStatus(true, it)
        }, {
          ToastUtil.showToast(it.msg?:"")
        })
    }

    override fun getVerifyCode(mobile: String) {
        BaseRequest.request(mIAccountFunctionApi.getVerifyCode(mobile), {
            mView.showSendVerifyCodeStatus(it.data.success, it.msg)
        }, {
            mView.showSendVerifyCodeStatus(false, it.msg)
        })
    }

    override fun getData(isRefresh: Boolean) {
    }
}